/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;


import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import impresos.DatosToImprimir;
import otros.Excel;
import otros.Fecha;
import impresos.ImpInforme;
import javax.swing.JOptionPane;
import otros.MiTableModel;
import tablasSql.Articulos;
import tablasSql.ConsultaBD;
import tablasSql.Familias;
import tablasSql.Stock;

/**
 *
 * @author FERRETERIA
 */
public class FReguArtFami extends javax.swing.JInternalFrame {
    boolean fuera;
    int filaSel;
  
    public FReguArtFami() {
        initComponents();
        AnchoColumnas();
        tabla.setColumnSelectionAllowed(false);
           
        Object [][] familias = Familias.getFamilias("");
        for (int i = 0; i < familias.length; i++)
            cbFamilia.addItem(familias[i][2]);
        
        bBorrar.setEnabled(true);
        reset();
        tabla.setAutoCreateRowSorter(true);
        
    }

    private void AnchoColumnas() {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)tabla.getColumnModel();       
        colModel.getColumn(0).setPreferredWidth(80);
        colModel.getColumn(1).setPreferredWidth(320);
    }
    
    private void ActualizarTabla() {     
        ConsultaBD.cursorEspera(this);
        Vector <String> camposT = new Vector<String>();
        Vector <String> camposS = new Vector<String>();
        camposT.add("Codigo");
        camposS.add("codigo");
        camposT.add("Detalle");
        camposS.add("detalle");
        camposT.add("Stock");
        camposS.add("stockactual");
        camposT.add("Nuevo Stock");
       
        MiTableModel datos = Articulos.getArticulos(camposS, camposT, cbFamilia.getSelectedItem().toString(), 
                    txtBusq.getText());
        
        datos.setEditable(new Boolean[]{false, false, false, true});
        tabla.setModel(datos);      
        tabla.setAutoCreateRowSorter(true);
        tabla.getColumnModel().getColumn(0).setCellRenderer(new tableCellRender.Texto(filaSel, fuera));
        tabla.getColumnModel().getColumn(1).setCellRenderer(new tableCellRender.Texto(filaSel, fuera));
        tabla.getColumnModel().getColumn(2).setCellRenderer(new tableCellRender.Stock(filaSel, fuera));
        tabla.getColumnModel().getColumn(3).setCellRenderer(new tableCellRender.Stock(filaSel, fuera));
        
        AnchoColumnas();
        bImprimir.setEnabled((tabla.getRowCount() > 0)? true : false);
        bExcel.setEnabled((tabla.getRowCount() > 0)? true : false);
        
        txtCantidad.setText(""+tabla.getRowCount());
        ConsultaBD.cursorNormal(this);
    }
    
    private void reset() {
        bImprimir.setEnabled(false);
        bExcel.setEnabled(false);
        bGuardar.setEnabled(false);
        txtCantidad.setText("");
        int tam = tabla.getRowCount();
        for (int i = 0; i < tam; i++) 
            ((DefaultTableModel)tabla.getModel()).removeRow(0);
        
        
    }

    private DatosToImprimir getDatosToImprimir() {
        DatosToImprimir d = new DatosToImprimir();
        d.setTabla(tabla);
        d.setFamilia(cbFamilia.getSelectedItem().toString());
        d.setNumCopias(Integer.parseInt(txtCantidad.getText()));
        return d;
    }
    
    private void guardar() {
       for (int i = 0; i < tabla.getRowCount(); i++) { 
           try {
            float valor  = Float.parseFloat(tabla.getValueAt(i, 3).toString().replace(",",".").trim());
            if (valor > -1)
               Stock.cambiarStock(tabla.getValueAt(i, 0).toString(),valor,new Fecha().getFechaSQL());
           } catch (Exception e) {
           }
       } 
    }
    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        bBuscar = new javax.swing.JButton();
        cbFamilia = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtBusq = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        bImprimir = new javax.swing.JButton();
        bBorrar = new javax.swing.JButton();
        txtCantidad = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        bExcel = new javax.swing.JButton();
        bGuardar = new javax.swing.JButton();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Regulacion de Stock por Familia");
        setToolTipText("");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jScrollPane1.setMinimumSize(new java.awt.Dimension(600, 250));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(600, 250));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Detalle", "Stock", "Nuevo Stock"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setColumnSelectionAllowed(true);
        tabla.setMinimumSize(new java.awt.Dimension(45, 0));
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tablaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tablaMouseExited(evt);
            }
        });
        tabla.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                tablaMouseMoved(evt);
            }
        });
        tabla.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tablaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);
        tabla.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        if (tabla.getColumnModel().getColumnCount() > 0) {
            tabla.getColumnModel().getColumn(0).setResizable(false);
            tabla.getColumnModel().getColumn(1).setResizable(false);
            tabla.getColumnModel().getColumn(2).setResizable(false);
            tabla.getColumnModel().getColumn(3).setResizable(false);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(600, 70));
        jPanel1.setPreferredSize(new java.awt.Dimension(450, 70));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Criterios de busqueda:   ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        jPanel1.add(jLabel1, gridBagConstraints);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        bBuscar.setMaximumSize(new java.awt.Dimension(71, 71));
        bBuscar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBuscar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(bBuscar, gridBagConstraints);

        cbFamilia.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbFamiliaItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel1.add(cbFamilia, gridBagConstraints);

        jLabel2.setText("Familia");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 31, 0, 0);
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel4.setText("Palabras claves");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        jPanel1.add(jLabel4, gridBagConstraints);

        txtBusq.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtBusq.setMinimumSize(new java.awt.Dimension(150, 25));
        txtBusq.setPreferredSize(new java.awt.Dimension(150, 25));
        txtBusq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtBusqMouseClicked(evt);
            }
        });
        txtBusq.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBusqKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
        jPanel1.add(txtBusq, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 175;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 10);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setPreferredSize(new java.awt.Dimension(60, 60));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        bImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/imprimir.png"))); // NOI18N
        bImprimir.setMaximumSize(new java.awt.Dimension(71, 71));
        bImprimir.setMinimumSize(new java.awt.Dimension(60, 60));
        bImprimir.setPreferredSize(new java.awt.Dimension(60, 60));
        bImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bImprimirActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel2.add(bImprimir, gridBagConstraints);

        bBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        bBorrar.setMaximumSize(new java.awt.Dimension(71, 71));
        bBorrar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBorrar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBorrarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
        jPanel2.add(bBorrar, gridBagConstraints);

        txtCantidad.setEditable(false);
        txtCantidad.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCantidad.setMinimumSize(new java.awt.Dimension(70, 20));
        txtCantidad.setPreferredSize(new java.awt.Dimension(70, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 13, 150);
        jPanel2.add(txtCantidad, gridBagConstraints);

        jLabel3.setText("Nº de Articulos");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 150);
        jPanel2.add(jLabel3, gridBagConstraints);

        bExcel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Excel2.png"))); // NOI18N
        bExcel.setMaximumSize(new java.awt.Dimension(71, 71));
        bExcel.setMinimumSize(new java.awt.Dimension(60, 60));
        bExcel.setPreferredSize(new java.awt.Dimension(60, 60));
        bExcel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bExcelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        jPanel2.add(bExcel, gridBagConstraints);

        bGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/guardar.png"))); // NOI18N
        bGuardar.setMaximumSize(new java.awt.Dimension(71, 71));
        bGuardar.setMinimumSize(new java.awt.Dimension(60, 60));
        bGuardar.setPreferredSize(new java.awt.Dimension(60, 60));
        bGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGuardarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(0, 40, 0, 0);
        jPanel2.add(bGuardar, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(jPanel2, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked

        filaSel = tabla.rowAtPoint(evt.getPoint());
        if (evt.getClickCount() == 2) {
        }

    }//GEN-LAST:event_tablaMouseClicked

    private void tablaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseExited
        fuera = true;
    }//GEN-LAST:event_tablaMouseExited

    private void tablaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseEntered
        fuera = false;
    }//GEN-LAST:event_tablaMouseEntered

    private void tablaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseMoved
        JTable aTable =  (JTable)evt.getSource();
        if (!fuera) {
            filaSel = aTable.rowAtPoint(evt.getPoint());
        }
    }//GEN-LAST:event_tablaMouseMoved

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        ActualizarTabla();
    }//GEN-LAST:event_bBuscarActionPerformed

    private void bImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bImprimirActionPerformed
        int op = JOptionPane.showOptionDialog(this, "¿Imprimir en PDF?", "Seleccione opción", 
                                                            JOptionPane.YES_NO_OPTION,                    
                                                            JOptionPane.QUESTION_MESSAGE,
                                                            null,    // null para icono por defecto.
                                                            null,   // null para YES, NO y CANCEL
                                                            "no");
        
        ImpInforme.imprime(getDatosToImprimir(), op==0?true:false );
      //reset();
    }//GEN-LAST:event_bImprimirActionPerformed

    private void bBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBorrarActionPerformed
        reset();
        
    }//GEN-LAST:event_bBorrarActionPerformed

    private void bExcelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bExcelActionPerformed
        ConsultaBD.cursorEspera(this);
        Excel.Exportar(tabla, cbFamilia.getSelectedItem().toString());
        ConsultaBD.cursorNormal(this);
    }//GEN-LAST:event_bExcelActionPerformed

    private void bGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGuardarActionPerformed
        guardar();
        ActualizarTabla();
    }//GEN-LAST:event_bGuardarActionPerformed

    private void tablaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tablaKeyReleased
        if ((evt.getKeyCode() == 10) && (!tabla.getValueAt(filaSel, 3).toString().equals("-1")))
            bGuardar.setEnabled(true);
    }//GEN-LAST:event_tablaKeyReleased

    private void cbFamiliaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbFamiliaItemStateChanged
        if (cbFamilia.getSelectedIndex() > 0)
            ActualizarTabla();
    }//GEN-LAST:event_cbFamiliaItemStateChanged

    private void txtBusqMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBusqMouseClicked
        txtBusq.selectAll();
    }//GEN-LAST:event_txtBusqMouseClicked

    private void txtBusqKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBusqKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtBusqKeyReleased

         
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBorrar;
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bExcel;
    private javax.swing.JButton bGuardar;
    private javax.swing.JButton bImprimir;
    private javax.swing.JComboBox cbFamilia;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtBusq;
    private javax.swing.JTextField txtCantidad;
    // End of variables declaration//GEN-END:variables
}
